package com.aaa.oauth;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.annotation.Resource;
import java.util.Collection;

@Configuration
// 作为资源服务
@EnableResourceServer
// 权限校验
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class OauthResourceConfig extends ResourceServerConfigurerAdapter {

     @Value("${security.oauth2.resource.jwt.key-value}")
     String keyValue;

    /**
     * http请求访问方式
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest()
                .authenticated();
        http.csrf().disable();
    }
    /**
     * jwtToken转换器
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey(keyValue);
        jwtAccessTokenConverter.setVerifierKey(keyValue);
        return jwtAccessTokenConverter;
    }
    /**
     * token 存储
     * @return
     */
    @Bean
    public TokenStore jwtTokenStore(){
        return new JwtTokenStore(jwtTokenConverter());
    }
    @Bean
    @Primary
    public DefaultTokenServices initTokenService() {
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setTokenStore(jwtTokenStore());
        return tokenServices;
    }

    @Resource
    ClientAuthenticationEntryPoint clientAuthenticationEntryPoint;
    @Resource
    ClientAccessDeniedHandler clientAccessDeniedHandler;
    @Bean
    public PasswordEncoder initPasswordEncode() {
        return new BCryptPasswordEncoder();
    }
   @Override
   public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
       resources.tokenServices(initTokenService());
       resources.resourceId("backservice");
       //令牌过期
       resources.authenticationEntryPoint(clientAuthenticationEntryPoint);
       resources.accessDeniedHandler(clientAccessDeniedHandler);
       super.configure(resources);
    }

}
